<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <el-card style="max-width: 100%">
    <el-button type="primary" class="button" @click="handleDialogValue()"
      >添加角色</el-button
    ><Dialog
      v-model="dialogVisible"
      :dialogTitle="dialogTitle"
      v-if="dialogVisible"
      @initUserList="getrolesList"
      :dialogTableValue="dialogTableValue"
    ></Dialog>
    <el-table :data="rolesList" border style="width: 100%">
      <el-table-column type="expand">
        <template v-slot="{ row }">
          <el-row
            :class="['dbbottom', i1 === 0 ? 'dbtop' : '', 'vcenter']"
            v-for="(item1, i1) in row.children"
            :key="item1.id"
          >
            <el-col :span="5">
              <el-tag
                closable
                @close="removeRightById(row.id, item1.id, row)"
                >{{ item1.authName }}</el-tag
              >
              <el-icon><ArrowRightBold /></el-icon>
            </el-col>
            <el-col :span="19">
              <el-row
                :class="[i2 === 0 ? '' : 'dbtop', 'vcenter']"
                v-for="(item2, i2) in item1.children"
                :key="item2.id"
              >
                <el-col :span="6">
                  <el-tag
                    type="success"
                    closable
                    @close="removeRightById(row.id, item2.id, row)"
                    >{{ item2.authName }}</el-tag
                  ><el-icon><ArrowRightBold /></el-icon
                ></el-col>
                <el-col :span="18">
                  <el-tag
                    type="warning"
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    closable
                    @close="removeRightById(row.id, item3.id, row)"
                    >{{ item3.authName }}</el-tag
                  >
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        :type="item.type"
        :prop="item.prop"
        :label="$t(`${item.label}`)"
        v-for="(item, index) in options"
        :key="index"
        ><template #default="{ row }" v-if="item.prop === 'action'">
          <el-tooltip
            class="box-item"
            effect="dark"
            content="编辑用户"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="primary"
              size="small"
              :icon="Edit"
              @click="handleDialogValue(row)"
              >编辑</el-button
            >
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="分配角色"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="warning"
              size="small"
              :icon="Setting"
              @click="handlePowerDialog(row)"
              >分配权限</el-button
            >
          </el-tooltip>
          <el-tooltip
            class="box-item"
            effect="dark"
            content="删除用户"
            placement="top"
            :enterable="false"
          >
            <el-button
              type="danger"
              size="small"
              :icon="Delete"
              @click="delUser(row)"
              >删除</el-button
            >
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <powerDialog
      v-model="powerDialogVisible"
      :powerdialogTitle="powerdialogTitle"
      v-if="powerDialogVisible"
      @initUserList="getrolesList"
      :powerdialogTableValue="powerdialogTableValue"
      :Powerrow="Powerrow"
    ></powerDialog>
  </el-card>
</template>

<script setup>
import Dialog from './components/dialog.vue'
import { ref } from 'vue'
import { getRoles, deleteUser, delUsePower } from '@/api/roles'
import { Delete, Edit, Setting } from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { isNull } from '@/utils/filters'
import { options } from './options'
import { useI18n } from 'vue-i18n'
import powerDialog from './components/powerDialog.vue'
const i18n = useI18n()
const rolesList = ref([])
const dialogTitle = ref('')
const dialogTableValue = ref({})
const dialogVisible = ref(false)
const powerdialogTitle = ref('')
const powerdialogTableValue = ref({})
const powerDialogVisible = ref(false)
const Powerrow = ref('')
const getrolesList = async () => {
  const res = await getRoles()
  rolesList.value = res
}
getrolesList()

const handlePowerDialog = (row) => {
  powerdialogTitle.value = '分配权限'
  powerdialogTableValue.value = {}
  Powerrow.value = row
  console.log(row)
  powerDialogVisible.value = true
}
const handleDialogValue = (row) => {
  if (isNull(row)) {
    dialogTitle.value = '添加用户'
    dialogTableValue.value = {}
  } else {
    dialogTitle.value = '编辑用户'
    dialogTableValue.value = JSON.parse(JSON.stringify(row))
  }
  dialogVisible.value = true
}
const delUser = (row) => {
  ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await deleteUser(row.id)
    ElMessage({
      message: i18n.t('message.updeteSuccess'),
      type: 'success'
    })
    getrolesList()
  })
}
const removeRightById = async (role, rightId, row) => {
  const confirmResult = await ElMessageBox.confirm(
    '此操作将永久删除该角色, 是否继续?',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).catch((err) => {
    return err
  })
  if (confirmResult !== 'confirm') {
    return ElMessage({ message: '取消删除', type: 'info' })
  }
  const { ...res } = await delUsePower(role, rightId)
  ElMessage({
    message: i18n.t('message.updeteSuccess'),
    type: 'success'
  })
  row.children = res
}
</script>

<style lang="scss" scoped>
.button {
  margin-bottom: 20px;
  width: 150px;
  height: 60px;
  font-size: large;
}
.el-tag {
  margin: 30px;
  width: 80px;
  height: 40px;
  margin-left: 40px;
}
.dbtop {
  border-top: 1px solid #e4e7ed;
}
.dbbottom {
  border-bottom: 1px solid #e4e7ed;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
